package com.tbig.playerproTest;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.jayway.android.robotium.solo.Solo;

@SuppressWarnings("unchecked")
public class TestAPK extends ActivityInstrumentationTestCase2 {
	private static final String TARGET_PACKAGE_ID = "com.tbig.playerpro"; // CHANGE
	private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.tbig.playerpro.MusicBrowserActivity"; // CHANGE
	private static Class launcherActivityClass;
	static {
		try {
			launcherActivityClass = Class
					.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	public TestAPK() throws ClassNotFoundException {
		super(TARGET_PACKAGE_ID, launcherActivityClass);
	}

	private Solo solo;

	@Override
	protected void setUp() throws Exception {		
		solo = new Solo(getInstrumentation(), getActivity());		
	}

	public void testPlaySongWMA() {
//		solo.sleep(3000); // wait for splash
		solo.clickOnText("Titel");
		solo.clickInList(1); // first item wma
		solo.sleep(5000); // playing 5sec
		solo.clickOnImageButton(8); // press pause
//		solo.sleep(1000); // wait before exit
	}

	public void testPlaySongMP3() {
//		solo.sleep(3000); // wait for splash
		solo.clickOnText("Titel");
		solo.clickInList(2); // second item mp3
		solo.sleep(5000); // playing 5sec
		solo.clickOnImageButton(8); // press pause
//		solo.sleep(1000); // wait before exit
	}

	public void testNextSong() {
//		solo.sleep(3000); //wait for splash
		solo.clickOnText("Titel");
		solo.clickInList(2);	//select mp3 song
		solo.sleep(5000);	//play 5 sec
		solo.clickOnImageButton(6); // next song
		solo.sleep(5000); //play 5 sec
		solo.clickOnImageButton(8); // pause
//		solo.sleep(1000); //wait before exist
	}

	public void testPlaylistCreate() {
		String playlist = "TestPlaylist";
//		solo.sleep(3000); //wait for splash
		solo.clickOnText("Titel");
		solo.clickLongInList(2); //select mp3 song
		solo.clickInList(4); //add to playlist
		solo.clickInList(2); //new playlist
		solo.clearEditText(0); //delete playlistname
		solo.enterText(0,playlist); //playlistname
		solo.clickOnButton("Speichern");
		solo.clickLongInList(1); //select wma song
		solo.clickInList(4); //add to playlist
		solo.clickOnText(playlist); //select playlist
		solo.clickOnText("Playlists"); //select menu playlist
		solo.clickOnText(playlist); //select playlist to play
		solo.clickInList(1);	//select song
		solo.sleep(5000); //play 5 sec
		solo.clickOnImageButton(8); // pause
//		solo.sleep(1000); //wait before exit
	}
	
	public void testPlaylistDelete() {
		String playlist = "TestPlaylist";
//		solo.sleep(3000); //wait for splash
		solo.clickOnText("Playlists");
		solo.clickLongOnText(playlist); //select playlist menu
		solo.clickInList(3); 	//delete playlist
//		solo.sleep(1000); //wait before exit
	}
	
	public void testEqualizer() {
//		solo.sleep(3000); // wait for splash
		solo.clickOnText("Titel");
		solo.clickInList(2); // second item mp3
		solo.sleep(2000); //play 2 sec
		solo.goBack();
		solo.clickOnMenuItem("Equalizer"); //select equalizer menu
		solo.clickOnText("Normal"); //change eq from normal
		solo.clickOnText("Dance");	//to Dance	
		solo.sleep(5000); //play 5 sec
		solo.clickOnText("Dance"); //change eq from dance
		solo.scrollUp();
		solo.clickOnText("Normal"); // to Normal
		solo.goBack();
		solo.sleep(2000); //play 2 sec
		solo.clickInList(2); // return to song
		solo.clickOnImageButton(8); // press pause
//		solo.sleep(1000); //wait before exit
	}
	
	public void testPlaySongMP3WithHeadsetPlugIn() {
		Log.d(getName(), "Please Plug in the Headset");
		solo.sleep(5000); // plug in headset
//		solo.sleep(3000); // wait for splash
		solo.clickOnText("Titel");
		solo.clickInList(2); // second item mp3
		solo.sleep(5000); // playing 5sec
		solo.clickOnImageButton(8); // press pause
//		solo.sleep(1000); // wait before exit
	}
	
	public void testVideoPlayer(){
//		solo.sleep(3000); // wait for splash
		solo.clickOnText("Video");
		solo.clickInList(1); // first video
		solo.sleep(5000); // playing 5sec
		solo.goBack();
//		solo.sleep(1000); // wait before exit
	}

	@Override
	public void tearDown() throws Exception {
		solo.finishOpenedActivities();
	}

	// Log.d(getName(), "" + solo.getCurrentImageButtons().size());
	// for(ImageButton b : solo.getCurrentImageButtons()) {
	// Log.i(getName(), b.getId() + "");
	// }
	// List<ImageButton> btnList = solo.getCurrentImageButtons();
	// for (int i = 0; i < btnList.size(); i++) {
	// ImageButton btn = btnList.get(i);
	// solo.clickOnImageButton(i);
	//		Log.d(getName(), "" + solo.getCurrentListViews().size());
//	Log.d(getName(), "" + solo.getCurrentTextViews( solo.getCurrentListViews().get(0)).size());
//	 Log.d(getName(), solo.getText(0).toString());
//	 Log.i(getName(), solo.getText(1).toString());
//	 Log.i(getName(), solo.getText(2).toString());
	// }
}